home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / squport.arc / SQ.C < prev    next >
Text File  |  1985-06-16  |  6KB  |  202 lines

  1. /* This program compresses a file without loosing information.
  2.  * The "usq" program is required to unsqueeze the file
  3.  * before it can be used.
  4.  *
  5.  * Typical compression rates are between 30 and 50 percent for text files.
  6.  *
  7.  * Squeezing a really big file takes a few minutes.
  8.  *
  9.  * Useage:
  10.  *    sq [file1] [file2] ... [filen]
  11.  *
  12.  * where file1 through filen are the names of the files to be squeezed.
  13.  * The file type (under CP/M or MS-DOS) is changed to ".SQ"; under UN*X,
  14.  * ".SQ" is appended to the file name. The original file name is stored
  15.  * in the squeezed file.
  16.  *
  17.  * If no file name is given on the command line you will be
  18.  * prompted for commands (one at a time). An empty command
  19.  * terminates the program.
  20.  *
  21.  * The transformations compress strings of identical bytes and
  22.  * then encode each resulting byte value and EOF as bit strings
  23.  * having lengths in inverse proportion to their frequency of
  24.  * occurrance in the intermediate input stream. The latter uses
  25.  * the Huffman algorithm. Decoding information is included in
  26.  * the squeezed file, so squeezing short files or files with
  27.  * uniformly distributed byte values will actually increase size.
  28.  */
  29.  
  30. /* CHANGE HISTORY:
  31.  * 1.3    Close files properly in case of error exit.
  32.  * 1.4    Break up long introductory lines.
  33.  * 1.4    Send introduction only to console.
  34.  * 1.4    Send errors only to console.
  35.  * 1.5  Fix BUG that caused a rare few squeezed files
  36.  *    to be incorrect and fail the USQ crc check.
  37.  *    The problem was that some 17 bit codes were
  38.  *    generated but are not supported by other code.
  39.  *    THIS IS A MAJOR CHANGE affecting TR2.C and SQ.H and
  40.  *    requires recompilation of all files which are part
  41.  *    of SQ. Two basic changes were made: tree depth is now
  42.  *    used as a tie breaker when weights are equal. This
  43.  *    makes the tree shallower. Although that may always be
  44.  *    sufficient, an error trap was added to cause rescaling
  45.  *    of the counts if any code > 16 bits long is generated.
  46.  * 1.5    Add debugging displays option '-'.
  47.  * 1.6  Fixed to work correctly under MP/M II.  Also shortened
  48.  *      signon message.
  49.  * 2.0    New version for use with CI-C86 compiler (CP/M-86 and MS-DOS)
  50.  * 2.1  Converted for use in MLINK
  51.  * 2.2  Converted for use with optimizing CI-C86 compiler (MS-DOS)
  52.  * 3.0  Generalized for UN*X use, changed output file naming convention
  53.  */
  54.  
  55. /* eject eject */
  56.  
  57. /*
  58.  * The following define MUST be set to the maximum length of a file name
  59.  * on the system "sq" is being compiled for.  If not, "sq" will not be
  60.  * able to check for whether the output file name it creates is valid
  61.  * or not.
  62.  */
  63.  
  64. #define FNM_LEN 14
  65. #define UNIX                /* comment out for CP/M, MS-DOS versions */
  66. #define SQMAIN
  67.  
  68. #define VERSION "3.1   12/19/84"
  69.  
  70. #include <stdio.h>
  71. #include "sqcom.h"
  72. #include "sq.h"
  73. #define FALSE 0
  74.  
  75. main(argc, argv)
  76. int argc;
  77. char *argv[];
  78. {
  79.     int i,c;
  80.     char inparg[128];    /* parameter from input */
  81.  
  82.     debug = FALSE;
  83.     printf("File squeezer version %s (original author: R. Greenlaw)\n\n", VERSION);
  84.  
  85.     /* Process the parameters in order */
  86.     for(i = 1; i < argc; ++i)
  87.         obey(argv[i]);
  88.  
  89.     if(argc < 2) {
  90.         printf("Enter file names, one line at a time, or type <RETURN> to quit.");
  91.         do {
  92.             printf("\n*");
  93.             for(i = 0; i < 16; ++i) {
  94.                 if((c = getchar()) == EOF)
  95.                     c = '\n';    /* fake empty (exit) command */
  96.                 if((inparg[i] = c) == '\n') {
  97.                     inparg[i] = '\0';
  98.                     break;
  99.                 }
  100.             }
  101.             if(inparg[0] != '\0')
  102.                 obey(inparg);
  103.         } while(inparg[0] != '\0');
  104.     }
  105. }
  106.  
  107. /* eject eject */
  108.  
  109. obey(p)
  110. char *p;
  111. {
  112.     char *q;
  113.     char outfile[128];    /* output file spec. */
  114.  
  115.     if(*p == '-') {
  116.         /* toggle debug option */
  117.         debug = !debug;
  118.         return;
  119.     }
  120.  
  121.     /* Check for ambiguous (wild-card) name */
  122.     for(q = p; *q != '\0'; ++q)
  123.         if(*q == '*' || *q == '?') {
  124.             printf("\nAmbiguous name %s ignored", p);
  125.             return;
  126.     }
  127.     /* First build output file name */
  128.     strcpy(outfile, p);        /* copy input name to output */
  129.  
  130.     /* Find and change output file suffix */
  131.  
  132.     if (strlen(outfile) + 3 > FNM_LEN) {    /* check for long file name */
  133.         q = outfile + FNM_LEN - 3;
  134.         *q = '\0';        /* make room for suffix */
  135.     }
  136.     else {
  137.         q = outfile + strlen(outfile);
  138. #ifndef UNIX
  139.         for(; --q >= outfile;)
  140.             if (*q == '.') {
  141.                 *q = '\0';    /* delete file type */
  142.                 break;
  143.             }
  144. #else
  145.         --q;
  146. #endif
  147.     }
  148.  
  149.     strcat(outfile, ".SQ");
  150.  
  151.     squeeze(p, outfile);
  152. }
  153.  
  154. /* eject eject */
  155.  
  156. squeeze(infile, outfile)
  157. char *infile, *outfile;
  158. {
  159.     int i, c,c2;
  160.     FILE *inbuff, *outbuff;        /* file buffers */
  161.  
  162.     printf("%s -> %s: ", infile, outfile);
  163.  
  164.     if(!(inbuff=fopen(infile, "rb"))) {
  165.         printf("Can't open %s for input pass 1\n", infile);
  166.         return;
  167.     }
  168.     if(!(outbuff=fopen(outfile, "wb"))) {
  169.         printf("Can't create %s\n", outfile);
  170.         fclose(inbuff);
  171.         return;
  172.     }
  173.  
  174.     /* First pass - get properties of file */
  175.     crc = 0;    /* initialize checksum */
  176.     printf("analyzing, ");
  177.     init_ncr();
  178.     init_huff(inbuff);   
  179.     fclose(inbuff);
  180.  
  181.     /* Write output file header with decoding info */
  182.     wrt_head(outbuff, infile);
  183.  
  184.     /* Second pass - encode the file */
  185.     printf("squeezing,");
  186.     if(!(inbuff=fopen(infile, "rb"))) {
  187.         printf("Can't open %s for input pass 2\n", infile);
  188.         goto closeout;
  189.     }
  190.     init_ncr();    /* For second pass */
  191.  
  192.     /* Translate the input file into the output file */
  193.     while((c = gethuff(inbuff)) != EOF)
  194.         putce(c, outbuff);
  195.     oflush(outbuff);
  196.     printf(" done.\n");
  197. closeall:
  198.     fclose(inbuff);
  199. closeout:
  200.     fclose(outbuff);
  201. }
  202.